﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace 生命体征测试仪
{    
    public partial class formMedical : formBase
    {
        CreativePacket ErWenPacket;
        private bool ErWen_ComListening = false;//是否没有执行完invoke相关操作              
        private bool ErWen_ComClosing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke 
        Queue ErWenQueue;
        bool bErWenPacketProc;
        private void tabErWen_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            if(SysConfig.ErWenCom!=null)
            {
                ErWenPacket = new CreativePacket();
                ErWenPacket.bErWen = true;
                SysConfig.ErWenCom.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ErWenCom_DataReceived);
                ErWenQueue = new Queue();
                bErWenPacketProc = true;
                ErWen_ComListening = false;
                ErWen_ComClosing = false;
                Thread th = new Thread(new ThreadStart(ErWenPacketProc));
                th.Start();
            }
        }

        private void tabErWen_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            if (SysConfig.ErWenCom != null)
            {
                ErWen_ComClosing = true;
                while (ErWen_ComListening)
                    Application.DoEvents();
                try
                {
                    SysConfig.ErWenCom.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(ErWenCom_DataReceived);
                }
                catch { }
                bErWenPacketProc = false;
            }
        }
        void ErWenPacketProc()
        {
            while (bErWenPacketProc)
            {
                if (ErWenQueue.Count != 0)
                {
                    CreativePacket ErWenPacket;
                    lock (ErWenQueue.SyncRoot)
                    {
                        ErWenPacket = (CreativePacket)ErWenQueue.Dequeue();
                    }
                    if (ErWenPacket.Header.Command == 0x72 && ErWenPacket.Header.type == 0x02)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            byte s = ErWenPacket.Data[0];
                            s = (byte)(s & 0xc0);
//                             string str;
//                             if (s == 0x80)
//                                 str = "正确测量";
//                             else if (s == 0xc0)
//                                 str = "测量出错";
//                             else
//                                 str = "未知测量状态";
                            MedicalResult.ErWen = ErWenPacket.Data[1] * 256 + ErWenPacket.Data[2];
                            MedicalResult.ErWen = MedicalResult.ErWen / 100;
                            bChange = true;
                            Debug.WriteLine(MedicalResult.ErWen.ToString());
                            ErWenResult.Text = MedicalResult.ErWen.ToString("F2").Substring(0,4);
                            //textBox1.AppendText(str + ":" + t.ToString("F2") + "℃\r\n");
                        }));
                    } 
                }
            }
        }
        private void ErWenCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (ErWen_ComClosing)
                return;
            ErWen_ComListening = true;
            int rxn = SysConfig.ErWenCom.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)SysConfig.ErWenCom.ReadByte();
                if (ErWenPacket.DataPacketed(rx))
                {
                    lock (ErWenQueue.SyncRoot)
                    {
                        ErWenQueue.Enqueue(ErWenPacket);
                    }
                    ErWenPacket = new CreativePacket();
                    ErWenPacket.bErWen = true;
                }
            }

            ErWen_ComListening = false;
        }
    }    
}
